/**
 * Created by Administrator on 2015/12/3.
 */
"use strict";
var _o2oServiceApi = require('../../../common/o2oServiceApi');
var o2oServiceApi = new _o2oServiceApi;
var F = require('../../../common/function');
var _ = require('underscore');

exports.organization_list = function *(obj_query) {
  let organizationResult = yield o2oServiceApi.post('shop', 'list', 1, obj_query);

  if (parseInt(organizationResult['result']['error']) == 1 || organizationResult.status == 400) {
    return F.returnMsg(300, organizationResult['message']['msg'], 1);
  } else {
    if ((organizationResult['result'].length && organizationResult['result'].length == 0) || (organizationResult['result']['list'] && organizationResult['result']['list'].length == 0)) {
      return F.returnMsg(300, '已经没有数据', 1, organizationResult['result']);
    }

    if (obj_query.vendorType == 2) {//如果是商家后台，则返回是否绑定店长标识
      for (let i in organizationResult['result']['list']) {
        let record = organizationResult['result']['list'][i];
        record = _.pick(record, 'name', 'address', 'tel', 'shopKeeper', 'id', 'uid', 'userName', 'district');
        record.area = yield F.array_2_codeArray(record.district, 'name');
        record.area = record.area.join(' ');
        delete record.district;
        if (record['uid'] && parseInt(record['uid']) > 0) {
          record.hasBoundUser = 1;
        } else {
          record.hasBoundUser = 0;
        }
        organizationResult['result']['list'][i] = record;
      }
    } else {//如果是总后台
      for (let i in organizationResult['result']['list']) {
        let record = organizationResult['result']['list'][i];
        record = _.pick(record, 'name', 'address', 'tel', 'shopKeeper', 'id', 'district');
        record.area = yield F.array_2_codeArray(record.district, 'name');
        record.area = record.area.join(' ');
        delete record.district;
        organizationResult['result']['list'][i] = record;
      }
    }
    return F.returnMsg(200, '查找成功', 1, organizationResult['result']);
  }
};
